package com.example.znbk.znbklibrary.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.znbk.znbklibrary.R;
import com.example.znbk.znbklibrary.dialog.PauseDialog;
import com.example.znbk.znbklibrary.widget.ScaleButton;

public class ExitDialog extends Dialog {


    public ExitDialog(Context context) {
        super(context);
    }

    public ExitDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected ExitDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context context;
        private String title;  //标题
        private String message;//提示消息
        private String sbtnExitText;//消极的
        private String sbtnContinueText;//积极的
        private DialogInterface.OnClickListener sbtnExitListener;//消极的监听
        private DialogInterface.OnClickListener sbtnContinueListener;//积极的监听

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setTitle(String title) {
            if (title == null) {
                this.title = "提醒";
            }
            this.title = title;
            return this;
        }

        public Builder setMessage(String message) {
            if (message == null) {
                this.message = "您没有填写提示信息哦";
            }
            this.message = message;
            return this;
        }

        public Builder setExitButton(String sbtnExitText, DialogInterface.OnClickListener sbtnExitListener) {
            if (sbtnExitText == null) {
                this.sbtnExitText = "退出";
            }
            this.sbtnExitText = sbtnExitText;
            this.sbtnExitListener = sbtnExitListener;

            return this;
        }

        public Builder setContinueButton(String sbtnContinueText, DialogInterface.OnClickListener sbtnContinueListener) {
            if (sbtnContinueText == null) {
                this.sbtnContinueText = "确定";
            }
            this.sbtnContinueText = sbtnContinueText;
            this.sbtnContinueListener = sbtnContinueListener;

            return this;
        }

        private TextView mTvTitle;  //标题
        private TextView mTvMessage;//提示信息
        private ScaleButton mSBtnExitTrain;//退出
        private ScaleButton mSBtnContinueTrain;//继续


        public PauseDialog create() {
            final PauseDialog dialog = new PauseDialog(context);
            View view = LayoutInflater.from(context).inflate(R.layout.layout_exit_dialog, null);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//加上这一句，取消原来的标题栏，没加这句之前，发现在三星的手机上会有一条蓝色的线
            dialog.setContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            mTvTitle =  view.findViewById(R.id.tv_title);
            mTvMessage =  view.findViewById(R.id.tv_message);
            mSBtnExitTrain =  view.findViewById(R.id.sbtn_exitTrain);
            mSBtnContinueTrain =  view.findViewById(R.id.sbtn_continueTrain);
            mTvTitle.setText(title);
            mTvMessage.setText(message);
            mSBtnExitTrain.setText(sbtnExitText);
            mSBtnContinueTrain.setText(sbtnContinueText);
            Window window = dialog.getWindow();
            window.setWindowAnimations(R.style.MyDialogScale);//设置动画效果
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            mSBtnExitTrain.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sbtnExitListener.onClick(dialog, Dialog.BUTTON_NEGATIVE);
                }
            });
            mSBtnContinueTrain.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sbtnContinueListener.onClick(dialog, Dialog.BUTTON_POSITIVE);
                }
            });
            dialog.setCanceledOnTouchOutside(false);//禁止点击dialog外部销毁弹窗
            dialog.setOnKeyListener(new OnKeyListener() {//禁止系统返回键值响应
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        return true;
                    } else {
                        return false;
                    }
                }
            });
            return dialog;
        }
    }
}